package xyz.wadewhy.basicstest.easyExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName ReadTest
 * @Description 业务用途
 * @Author zhongzihao
 * @Date 2022/1/7 5:03 下午
 * @Version 1.0
 **/
@Slf4j(topic = "c.Test")
class RecordExcelListener extends AnalysisEventListener<ActivityTeacherRecommendRecordContentVo> {

    private final ProvinceMapper provinceMapper;

    /**
     * 批处理阈值
     */
    private static final int BATCH_COUNT = 1000;
    /**
     * 存放数据
     */
    List<ActivityTeacherRecommendRecordContentVo> list = new ArrayList<ActivityTeacherRecommendRecordContentVo>(BATCH_COUNT);

    public RecordExcelListener(ProvinceMapper provinceMapper) {
        this.provinceMapper = provinceMapper;
    }

    public static final String SQL = "INSERT INTO `ep_activity_teacher_recommend_record` ( `record_id`, `votes`, `teacher_name`, `group`, `avatar`, `title`, `cover_image`, `web_cover_image`, `content`, `create_time`, `update_time`, `version`, `deleted`) VALUES \n" +
            "\t('%s',%s,'%s',%s,'%s','%s','%s','%s','%s','2022-01-07 10:05:03','2022-01-07 14:14:58',0,0);";

    @Override
    public void invoke(ActivityTeacherRecommendRecordContentVo province, AnalysisContext context) {
        list.add(province);
        //内存存放超过多少条数据，则先存放到数据库中；保存完后，清空内存
        if (list.size() >= BATCH_COUNT) {
            long beforeTime = System.currentTimeMillis();
            saveData();
            list.clear();//清空内存
            System.out.println("处理1000数据所需的时间：" + (System.currentTimeMillis() - beforeTime));
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();//解析完成后需要调用一次，不大于BATCH_COUNT就没处理
    }

    /**
     * 保存数据
     */
    private void saveData() {
        for (ActivityTeacherRecommendRecordContentVo it : list) {
            it.setImages(Arrays.asList(it.getImageList().split(",")));
            it.setWebImages(Arrays.asList(it.getWebImageList().split(",")));
            ActivityTeacherRecommendRecordContent record = new ActivityTeacherRecommendRecordContent();
            BeanUtils.copyProperties(it, record);
            String content = JSON.toJSONString(record);
            String sql1 = String.format(SQL, it.getRecordId(), it.getVotes(), it.getTeacherName(), it.getGroup(), it.getAvatar(), it.getTitle(), it.getCoverImage(), it.getWebCoverImage(), content);
            System.err.println(sql1);
        }
    }
}

class Test {
    public static void main(String[] args) {
        String fileName = "/Users/wadewhy/Desktop/gai.xlsx";
        long beforeTime = System.currentTimeMillis();
        EasyExcel.read(fileName, ActivityTeacherRecommendRecordContentVo.class, new RecordExcelListener(new ProvinceMapper())).sheet().doRead();
        System.out.println("读取数据所花的时间：" + (System.currentTimeMillis() - beforeTime) + "毫秒");
    }
}